/* gm_utils.h -- Utilidades diversas
 *
 * Copyright (C) 2008, Diogo F. S. Ramos <diogo.ramos@usp.br>
 * GEAR - Grupo de Estudos Avançados em Robótica
 * <http://www.sel.eesc.usp.br/gear>
 *
 * This file is part os GEARMedula.
 * 
 * GEARMedula is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 3 of the
 * License.
 * 
 * GEARMedula is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#ifndef GM_UTILS_H_
#define GM_UTILS_H_

#include <stdlib.h>

#define MAX_MS_VEL

typedef struct gm_matrix_t gm_matrix_t;
typedef struct gm_args_t gm_args_t;

/** Estrutura para registrar o estado das variáveis passadas pela
 * linha de comando  */
struct gm_args_t
{
    int server_port;
    int server_real;
    int f_server_display;

    char * serial_path;
};

/** Uma matriz. */
struct gm_matrix_t
{
    int n_row;
    int n_col;
    float ** matrix;
};

#define XMALLOC( type, num )				\
    ( (type*) (xmalloc( sizeof(type) * num ) ) );
#define XREALLOC( type, num, ptr )				\
    ( (type*) xrealloc( (ptr), (sizeof(type) * (num))));
#define XFREE( stale )				\
    ( xfree( stale ) );

void * xmalloc( size_t num );
void * xrealloc( void *ptr, size_t num );
void xfree( void *ptr );

void xerror_warning( const char *message, ... );
void xerror_error( const char *message, ... );
void xerror_fatal( const char *message, ... );
void xerror_info( const char *message, ... );
    
void gm_matrix_mult_raw( const int n_row_A, const int n_col_A, const float ** matrix_A, const int n_row_X, const int n_col_X, const float ** matrix_X, float ** matrix_B );
gm_matrix_t * gm_matrix_new(const int n_row, const int n_col);
void gm_matrix_set( gm_matrix_t * mat, const int x, const int y, const float value );
float gm_matrix_get( gm_matrix_t * mat, const int x, const int y );
void gm_matrix_mult( const gm_matrix_t * matrix_A, const gm_matrix_t * matrix_X, gm_matrix_t * matrix_B );
void gm_matrix_clean( gm_matrix_t * matrix );
void gm_matrix_print( gm_matrix_t * mat );

int gm_args_parse( int argc, char *argv[],  gm_args_t * args_passed );

void gm_utils_str2int( int * num, char * str );
void gm_utils_int2str( char * str, const int num, const int size );

void gm_utils_float2str( char * str, const float num, const int size );

int gm_utils_vec_get_next_int( const char * msg, int * iter );
float gm_utils_vec_get_next_float( const char * msg, int * iter );

char * gm_utils_strcpy( const char * buff );

#endif /* GM_UTILS_H_ */
